/**
 * @description 获取设备导航栏相关的数据
 */

export function getNavigationInfo() {
  let menuButtonInfo = uni.getMenuButtonBoundingClientRect() || {}
  // 头条小程序，右上方会有一个反馈按钮
  if (process.env.VUE_APP_PLATFORM === 'mp-toutiao') {
    try {
      // 抖音全小程序不是custom布局，调用下面接口会报错
      // eslint-disable-next-line no-undef
      menuButtonInfo = tt.getCustomButtonBoundingClientRect().capsule
    } catch (error) {
      menuButtonInfo = uni.getMenuButtonBoundingClientRect() || {}
    }
  }
  const sysInfo = uni.getSystemInfoSync()
  const {
    statusBarHeight,
    titleBarHeight,
    screenWidth
  } = sysInfo
  let statusHeight = 0
  const platform = process.env.VUE_APP_PLATFORM
  if (platform === 'mp-alipay') {
    statusHeight = titleBarHeight
  } else {
    statusHeight = statusBarHeight
  }
  const menuHeight = menuButtonInfo.height || 40
  const menuTop = menuButtonInfo.top || 10
  const menuPaddingTop = menuTop - statusHeight
  const menuRight = screenWidth - (menuButtonInfo.right || screenWidth)
  const menuWidth = (menuButtonInfo.width || 0) + menuRight * 2
  const navBarHeight = menuHeight + menuPaddingTop * 2
  return {
    // 总导航栏高度（包括状态栏）
    navigationHeight: navBarHeight + statusHeight,
    // 胶囊按钮距离屏幕右边的距离
    menuRight,
    // 胶囊按钮的宽度（包括距离右边的距离）
    menuWidth,
    // 导航栏主体高度（不包括状态栏）
    navBarHeight,
    // 状态栏高度（兼容刘海屏）
    statusBarHeight: statusHeight
  }
}
